package com.spark.mooc.ch2_scala.part02_objectOrientedProgramming

import scala.io.StdIn.{readChar, readLine}

/**
 * @description:
 * @time: 2020/11/24 18:53
 * @author: lhy
 */


object TestMatch {
    def main(args: Array[String]): Unit = {
        print("please input the score:")
        val grade = readChar()
        grade match {
            case 'A' => println("85-100")
            case 'B' => println("60-84")
            case 'C' => println("30-59")
            case 'D' => println("0-29")
            case _ => println("error input!")
        }

        print("please input a country:")
        val country = readLine()
        country match {
            case "China" => println("中国")
            case "America" => println("美国")
            case "Japan" => println("日本")
            case _ => println("我不认识")
        }

        for (elem <- List(6,9.2,44, "com/spark","hadoop",true)){
            val str = elem match {
                case i:Int => i + " is an int value."    //匹配整型的值，并赋给i
                case d:Double => d + " is a double value."
                case "com/spark" => "spark is found."
                case s:String => s + " is a string value."
                case b:Boolean => b + " is a boolean value."
                case _ => "unexpected value"+ elem
            }
            println(str)
        }

        // case 里面可以有守卫式语句
        for (elem <- List(1,2,3,4,5)){
            elem match {
                case _ if(elem%2 == 0) => println(elem+" is even.")
                case _ => println(elem + " is odd.")
            }
        }

    }
}
